<template>
  <div id="app">
    <a-input
      placeholder="请输入任务"
      class="my_ipt"
      :value="inputValue"
      @change="
        e => {
          chageInputValue(e)
        }
      "
    />
    <a-button type="primary" @click="addList">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @click="changeChecked(item.id)">{{
          item.info
        }}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="deleteById(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>未完成：{{ unDoneNum }} 条</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button
            :type="viewkey === 'all' ? 'primary' : 'default'"
            @click="changeButton('all')"
            >全部</a-button
          >
          <a-button
            :type="viewkey === 'undone' ? 'primary' : 'default'"
            @click="changeButton('undone')"
            >未完成</a-button
          >
          <a-button
            :type="viewkey === 'done' ? 'primary' : 'default'"
            @click="changeButton('done')"
            >已完成</a-button
          >
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clear">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'app',
  data() {
    return {}
  },
  methods: {
    // 删除待办事项
    deleteById(id) {
      this.$store.commit('deleteList', id)
    },
    // 输入框中内容发生改变，触发函
    chageInputValue(e) {
      // console.log(e.target.value)
      this.$store.commit('editInputValue', e.target.value)
    },
    // 添加待办事项
    addList() {
      if (this.inputValue === '') {
        return this.$message.warn('文本框的内容不能为空!')
      }
      this.$store.commit('addList')
    },
    // 清空已完成
    clear() {
      this.$store.commit('clearDone')
    },
    // 待办事项的复选框选中状态取反
    changeChecked(id) {
      this.$store.commit('changeDone', id)
    },
    // 修改面板对应的值
    changeButton(value) {
      this.$store.commit('chageViewKey', value)
    }
  },
  created() {
    this.$store.dispatch('AgetList')
  },
  computed: {
    ...mapState(['list', 'inputValue', 'viewkey']),
    ...mapGetters(['unDoneNum', 'infoList'])
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
  width: 620px;
  margin: 5px auto;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 600px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
